package com.tidc.seaserverservice.service.impl;

import com.tidc.seaservermapper.dao.ShopDao;
import com.tidc.seaservermodel.entity.Fres;
import com.tidc.seaservermodel.entity.Shop;
import com.tidc.seaservermodel.exception.TidcBadParent;
import com.tidc.seaservermodel.util.SpringUtil;
import com.tidc.seaserverservice.service.SupervisorShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.Transient;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @author ：冯涛滔
 * @date ：Created in 2020/9/22 9:16
 * @description：
 * @modified By：
 * @version:
 */
@Service
@Slf4j
public class SupervisorShopServiceImpl implements SupervisorShopService {
    @Resource
    private ShopDao shopDao;
    @Transactional(rollbackFor = {SQLException.class})
    @Override
    public Fres<String> addCommodity(Shop shop, Authentication authentication) throws SQLException {
        shop.setUserId(Integer.valueOf(authentication.getName()));
        shopDao.insert(shop);
        return Fres.Success();
    }

    @Override
    public Fres<Map<Object, Object>> getShopList(String query,int pageSize, int pageNum, Authentication authentication) throws TidcBadParent {
        int count = shopDao.getShopListCount(query,Integer.parseInt(authentication.getName()));
        Map<Object, Object> map = SpringUtil.pagingPrepare(pageSize, pageNum, count);
        List<Shop> supervisorList = shopDao.getSupervisorList(query,(pageNum - 1) * pageSize, pageSize, Integer.parseInt(authentication.getName()));
        map.put("list",supervisorList);
        return Fres.Success(map,"查询成功");
    }
    @Override
    @Transactional(rollbackFor = {SQLException.class})
    public Fres<String> updateIsShow(Shop shop, Authentication authentication) throws SQLException, TidcBadParent {
        shop.setUserId(Integer.valueOf(authentication.getName()));
        shopDao.update(shop);
        return Fres.Success();
    }

    @Override
    public Fres<Shop> getShopById(int id, Authentication authentication) {
        Shop shop = shopDao.queryById(id, Integer.parseInt(authentication.getName()));
        return Fres.Success(shop,"成功");
    }
}
